package org.example.edusmart.controller.Teacher;

import jakarta.servlet.http.HttpServletRequest;
import org.example.edusmart.pojo.WorkAnswerPojo;
import org.example.edusmart.pojo.WorkPojo;
import org.example.edusmart.request.TeaOrStu.*;
import org.example.edusmart.service.WorkService;
import org.example.edusmart.utils.AliOSSUtil;
import org.example.edusmart.utils.GetTokenInfo;
import org.example.edusmart.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/TeacherWork")
public class TeaWorkController {

    @Autowired
    private GetTokenInfo getTokenInfo;

    @Autowired
    private WorkService workService;

    @Autowired
    private AliOSSUtil aliOSSUtil;

    /**
     * 布置作业
     */
    @PostMapping("/assign/")
    @PreAuthorize("hasAuthority('teacher_permission')")
    public Result assignWork(HttpServletRequest request, AssignWorkPojo assignWork) throws IOException {
        String strUserId = getTokenInfo.getUserIdByJwt(request);
        int userId = Integer.parseInt(strUserId);
        int courseId = assignWork.getCourseId();
        VerifyPojo verify = new VerifyPojo();
        verify.setCourseId(courseId);
        verify.setUserId(userId);
        // 查询课程是否由教师创建，检验教师发布作业的权限
        int result = workService.verifyWork(verify);
        if (result != 0) {
            MultipartFile file = assignWork.getFile();
            String workUrl = aliOSSUtil.upload(file);
            String workName = assignWork.getWorkName();
            LocalDateTime deadline = LocalDateTime.parse(assignWork.getDeadline());
            WorkPojo work = new WorkPojo();
            work.setWorkName(workName);
            work.setWorkUrl(workUrl);
            LocalDateTime createTime=LocalDateTime.now();
            work.setCreateTime(createTime);
            work.setDeadline(deadline);
            workService.assignWork(work);
            int workId = work.getWorkId();
            workService.linkWork(courseId, workId);
            return new Result(200,"作业发布成功");
        } else{
            return new Result(404,"您没有权限");
        }
    }

    /**
     * 获取未批改的作业
     */
    @PostMapping("/uncorrected/")
    @PreAuthorize("hasAuthority('teacher_permission')")
    public Result uncorrectedWork(HttpServletRequest request, @RequestBody WorkIdPojo work){
        int workId = work.getWorkId();
        List<HashMap<String, Object>>  data = workService.getUncorrectedWork(workId);
        return new Result(data, 200,"获取待批改作业成功");
    }

    /**
     * 批改作业
     */
    @PostMapping("/correct/")
    @PreAuthorize("hasAuthority('teacher_permission')")
    public Result correctWork(@RequestBody WorkAnswerIdPojo answer){
        int workanswerId = answer.getWorkAnswerId();
        System.out.println(workanswerId);
        List<HashMap<String, Object>> data = workService.correctWork(workanswerId);
        return new Result(data, 200,"批改作业成功");
    }

    /**
     * 给分
     */
    @PostMapping("/score/")
    @PreAuthorize("hasAuthority('teacher_permission')")
    public Result correctWork(HttpServletRequest request, @RequestBody WorkGradePojo workGrade){
//        String strUserId = getTokenInfo.getUserIdByJwt(request);
//        int userId = Integer.parseInt(strUserId);
        int workanswerId = workGrade.getWorkanswerId();
        int grade = workGrade.getGrade();
        workService.scoreWork(workanswerId, grade);
        return new Result(200,"评分作业成功");
    }
}
